package com.jixiang.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jixiang.dictionary.dao.DsLayoutMenuDao;
import com.jixiang.model.common.dtos.ResponseResult;
import com.jixiang.model.dictionary.entity.DsLayoutMenu;
import com.jixiang.dictionary.service.DsLayoutMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.jixiang.common.constants.RedisConstants.REDIS_PREX_DSLAYOUTMENU;

/**
 * (DsLayoutMenu)表服务实现类
 *
 * @author makejava
 * @since 2023-10-15 17:00:56
 */
@Service("dsLayoutMenuService")
public class DsLayoutMenuServiceImpl extends ServiceImpl<DsLayoutMenuDao, DsLayoutMenu> implements DsLayoutMenuService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ResponseResult appGetDsLayoutMenu() {
        String jsonObj = stringRedisTemplate.opsForValue().get(REDIS_PREX_DSLAYOUTMENU);
        if(StrUtil.isNotBlank(jsonObj)){
            List<DsLayoutMenu> list = JSONUtil.toList(jsonObj, DsLayoutMenu.class);
            return ResponseResult.okResult(list);
        }
        List<DsLayoutMenu> list = query().orderByAsc("priority").list();
        JSONArray objects = JSONUtil.parseArray(list);
        stringRedisTemplate.opsForValue().set(REDIS_PREX_DSLAYOUTMENU,objects.toString());
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult adminGetDsLayoutMenu() {
        List<DsLayoutMenu> list = query().orderByAsc("priority").list();
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult addDsLayoutMenu(DsLayoutMenu dsLayoutMenu) {
        stringRedisTemplate.delete(REDIS_PREX_DSLAYOUTMENU);
        boolean save = save(dsLayoutMenu);
        stringRedisTemplate.delete(REDIS_PREX_DSLAYOUTMENU);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult getLayoutMenuById(String id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult updateLayoutMenu(DsLayoutMenu dsLayoutMenu) {
        stringRedisTemplate.delete(REDIS_PREX_DSLAYOUTMENU);
        boolean b = updateById(dsLayoutMenu);
        stringRedisTemplate.delete(REDIS_PREX_DSLAYOUTMENU);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult deleteLayoutMenuDataById(String id) {
        stringRedisTemplate.delete(REDIS_PREX_DSLAYOUTMENU);
        boolean b = removeById(id);
        stringRedisTemplate.delete(REDIS_PREX_DSLAYOUTMENU);
        return ResponseResult.okResult("删除成功");
    }


}

